package com.cvmars.jianyou.module.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.config.MyConfig;
import com.cvmars.jianyou.model.ImageUploadModel;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.module.activity.FindTopicActivity;
import com.cvmars.jianyou.module.activity.PersonHomeActivity;
import com.cvmars.jianyou.module.base.BaseImagePagerActivity;
import com.cvmars.jianyou.module.model.FindModel;
import com.cvmars.jianyou.module.model.TopicModel;
import com.cvmars.jianyou.ui.CircleImageView;
import com.cvmars.jianyou.utils.DensityUtils;
import com.cvmars.jianyou.utils.ImageUtils;
import com.cvmars.jianyou.utils.UtilHelper;

import java.io.Serializable;
import java.util.List;

/**
 * Created by hehaifeng on 2018/12/23.
 */

public class FindAdapter extends BaseQuickAdapter<FindModel, BaseViewHolder> {


    Context mContext;

    public FindAdapter(Context context, int layoutResId, @Nullable List<FindModel> data) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, FindModel item) {


        helper.setText(R.id.findContent, item.text)
                .setText(R.id.find_time, UtilHelper.forumFriendlyTime(item.create_at))
                .setText(R.id.find_time_ni, UtilHelper.forumFriendlyTime(item.create_at))
                .setText(R.id.txt_comment_number, item.comment_total + "")
                .setText(R.id.txt_comment_dianzhan, item.like_total + "");


        final UserModel customer =
                item.customer;

        int position = helper.getAdapterPosition();

        helper.getView(R.id.rl_info).setVisibility(!item.is_hidden_name ? View.VISIBLE : View.GONE);
        helper.getView(R.id.rl_nickname).setVisibility(item.is_hidden_name ? View.VISIBLE : View.GONE);

        final List<TopicModel> topic = item.topic;

        if (topic != null && topic.size() > 0) {
            helper.getView(R.id.txt_tuiguang).setVisibility(View.VISIBLE);
            helper.setText(R.id.txt_tuiguang, "#" + item.topic.get(0).name + "#");

            helper.getView(R.id.txt_tuiguang).setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(mContext, FindTopicActivity.class);
                    intent.putExtra("model", topic.get(0));
                    mContext.startActivity(intent);
                }
            });
        } else {
            helper.getView(R.id.txt_tuiguang).setVisibility(View.GONE);
        }

        if (customer != null) {
            helper.setText(R.id.findUserName, customer.name)
                    .setText(R.id.find_address, customer.address_home)
                    .setText(R.id.nicename, "@" + item.customer.address_home + "的娃");
            helper.getView(R.id.iv_vip).setVisibility(item.customer.is_rut ? View.VISIBLE : View.GONE);
            CircleImageView image = helper.getView(R.id.findheadImg);
            ImageView ivSex = helper.getView(R.id.iv_sex);
            if (customer.gender == 1) {
                ivSex.setImageResource(R.drawable.ic_male);
            } else {
                ivSex.setImageResource(R.drawable.ic_women);
            }
            ImageUtils.loadImage(mContext, customer.avatar_url, image);
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    onGoHome(customer.id);
                }
            });
        }

        RecyclerView gridView = helper.getView(R.id.find_grid);
        RelativeLayout rl_image = helper.getView(R.id.rl_image);
        gridView.setLayoutManager(new GridLayoutManager(mContext, 3));

        ImageView ivPic = helper.getView(R.id.iv_image);


        ivPic.setVisibility(View.GONE);
        gridView.setVisibility(View.VISIBLE);
        final List<ImageUploadModel> images = item.images;
        rl_image.setVisibility(View.VISIBLE);
        if (images != null) {

            if (images.size() > 1) {

                FindImgAdapter imgAdapter = new FindImgAdapter(mContext, R.layout.item_find_img, images);
                imgAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                        Bundle bundle = new Bundle();
                        bundle.putSerializable(MyConfig.INTENT_DATA_URL, (Serializable) images);
                        Intent intent = new Intent(mContext, BaseImagePagerActivity.class);
                        intent.putExtra("position", position);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                });
//                gridView.addItemDecoration(new SpacesItemDecoration(DensityUtils.dip2px(mContext, 3)));
                gridView.setAdapter(imgAdapter);
            } else if (images.size() == 1) {
                ivPic.setVisibility(View.VISIBLE);
                gridView.setVisibility(View.GONE);
                ImageUtils.loadImage(mContext, images.get(0).url, ivPic);


                int width = Integer.parseInt(images.get(0).w);
                int height = Integer.parseInt(images.get(0).h);

                if(height!=0 && width!=0){
                    if (width > height) {
                        ivPic.getLayoutParams().width = DensityUtils.dp2px(mContext, 180);
                        ivPic.getLayoutParams().height = DensityUtils.dp2px(mContext, 180) * height /  width;

                    }else{

                        ivPic.getLayoutParams().height = DensityUtils.dp2px(mContext, 180);
                        ivPic.getLayoutParams().width = DensityUtils.dp2px(mContext, 180) * width /  height;

                    }

                }

                ivPic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Bundle bundle = new Bundle();
                        bundle.putSerializable(MyConfig.INTENT_DATA_URL, (Serializable) images);
                        Intent intent = new Intent(mContext, BaseImagePagerActivity.class);
                        intent.putExtra("position", 0);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                });
            } else {
                rl_image.setVisibility(View.GONE);
            }
        } else {
            rl_image.setVisibility(View.GONE);
        }
    }


    private void onGoHome(String id) {

        Intent intent = new Intent(mContext,
                PersonHomeActivity.class);
        intent.putExtra("userid", id);
        mContext.startActivity(intent);
    }


}
